package com.eshin.mbean;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.Notification;
import javax.management.ObjectName;

import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http11.Http11AprProtocol;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.coyote.http11.Http11Protocol;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.jmx.export.notification.NotificationPublisher;
import org.springframework.jmx.export.notification.NotificationPublisherAware;


//继承接口的形式按接口暴露方法，spring-jmx中的assembler将无效
@Aspect
public class MyObject implements /**MyObjectMBean**/ NotificationPublisherAware{ 
	
private long id;  //private 变量不提供get方法将jmx将不能读取
private String name;  
private NotificationPublisher publisher; 
//public MyObject() {  
//    super();  
//}  
//  
//public MyObject(long id, String name) {  
//    super();  
//    this.id = id;  
//    this.name = name;  
//}  

public long getId() {  
    return id;  
}  

public void setId(long id) {  
    this.id = id;  
}  

public String getName() {  
    return name;  
}  

public void setName(String name) {  
    this.name = name;  
}  

public String show(int count) {  
    StringBuffer sb = new StringBuffer().append("id=").append(id).append(  
            ", name=").append(name); 
//获取tomcat端口号
	MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
	ObjectName name;
	Server server = null;
	try {
		name = new ObjectName("Catalina", "type", "Server");
		server = (Server) mBeanServer.getAttribute(name, "managedResource");
	} catch (Exception e) {
		e.printStackTrace();
	}
	// Server server = org.apache.catalina.ServerFactory.getServer();
	Service[] services = server.findServices();
	for (Service service : services) {
		for (Connector connector : service.findConnectors()) {
			ProtocolHandler protocolHandler = connector.getProtocolHandler();
			if (protocolHandler instanceof Http11Protocol || protocolHandler instanceof Http11AprProtocol
					|| protocolHandler instanceof Http11NioProtocol) {
				int serverPort = connector.getPort();
				System.out.println("HTTP Port: " + serverPort);
			}
		}
	}

//    发送jmx通知，由NoticeListener监听
    Notification notification = new Notification("show", this, count,"publish:"+count+" times");
//    notification.setUserData(user);
    this.publisher.sendNotification(notification);
    return sb.toString();  
}
public String test(){
	System.out.println("i am test");
	return "test";
}

@Override
public void setNotificationPublisher(NotificationPublisher notificationPublisher) {
	this.publisher = notificationPublisher;
}
@Pointcut("execution(* com.eshin.listener.*.*(..))")
private void anyMethod() {}//声明一个切入点	
@Before("anyMethod() && args(notification,handback)")//定义前置通知
public void doAccessCheck(Notification notification, Object handback) {
	System.out.println("bofore doing "+notification);
}	
@AfterReturning(pointcut="anyMethod()",returning="revalue")//定义后置通知
public void doReturnCheck(String revalue) {
	System.out.println("come from "+revalue);
}
@AfterThrowing(pointcut="anyMethod()", throwing="ex")//定义例外通知
public void doExceptionAction(Exception ex) {
	System.out.println("exception occur:"+ex.getMessage());
}
@After("anyMethod()")//定义最终通知
public void doReleaseAction() {
	System.out.println("done");
}
@Around("anyMethod()")//环绕通知
public void doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
	System.out.println("start...");
	 pjp.proceed();
	 System.out.println("finish...");
}

}  